﻿using DAO.db;
using Entity.user;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;

namespace DAO.user
{
   /// <summary>
    /// 用户表方法
   /// </summary>

    public class UserDao
    {
       /// <summary>
       /// 用户注册
       /// </summary>
       /// <param name="user"></param>
        /// <returns>10表示保存成功，否则保存失败</returns>
       public int Register(User user)
       {
           int ret = 0;
           user.ID = Guid.NewGuid().ToString();
           user.Status = 1;
           using (EFDbContext dbContext = new EFDbContext())
           {
               dbContext.Users.Add(user);
               dbContext.SaveChanges();
               ret = 10;
           }
           return ret;
       }




       /// <summary>
       /// 根据ID查询用户信息
       /// </summary>
       /// <param name="ID"></param>
       /// <returns></returns>
       public User GetByID(string ID)
       {
           User l = new User();
           using (EFDbContext dbContext = new EFDbContext())
           {
               l = dbContext.Users.Find(ID);
           }
           return l;
       }

       /// <summary>
       /// 保存修改用户信息
       /// </summary>
       /// <param name="ID"></param>
       /// <returns>10表示保存成功，否则保存失败</returns>
       public int Save(User ww )
       {
           int ee = 0;
           using (EFDbContext dbContext = new EFDbContext())
           {
               dbContext.Entry(ww).State = EntityState.Modified;
               dbContext.SaveChanges();
               ee = 1;
           }
           return ee;
       }




       /// <summary>
       /// 根据用户账户密码显示是否登陆成功
       /// </summary>
       /// <param name="ID"></param>
       /// <returns>返回100表示登陆成功</returns>
       public int Login(string Account ,string Password)
       {
           int tt = 0;

           using (EFDbContext dbContext = new EFDbContext())
           {
               var query = dbContext.Users.AsQueryable();

               if (!string.IsNullOrEmpty(Account))
               {
                   query.Where(t => t.Accounts == Account);
               }
               var ls = query.ToList();
               if (ls.Count > 0)
               {
                   if (ls[0].Password == Password)
                   {
                       return 100;
                   }
               }
           }

           return tt;

       }




       /// <summary>
       /// 查询用户列表
       /// </summary>
       /// <returns></returns>

       public List<User> GetList(string ShouJi)
       {
           List<User> i = new List<User>();
           using (EFDbContext dbContext = new EFDbContext())
           {
               var query = dbContext.Users.AsQueryable();

               if (!string.IsNullOrEmpty(ShouJi))
               {
                   query.Where(t => t.Phone==ShouJi);
               }
               i = query.ToList();
           }
           return i;
       }




       /// <summary>
       /// 冻结账户
       /// </summary>
       /// <param name="ID"></param>
       /// <returns>1表示停用成功，否则停用失败</returns>
       public int Frozen(string ID)
       {
           int ret = 0;
           using (EFDbContext dbContext = new EFDbContext())
           {
               var g = dbContext.Users.Find(ID);
               if (g != null)
               {
                   g.Status = 0;
                   dbContext.Entry(g).State = EntityState.Modified;
                   dbContext.SaveChanges();
                   ret = 1;
               }
           }
           return ret;
       }

       /// <summary>
       /// 解封账户
       /// </summary>
       /// <param name="ID"></param>
       /// <returns>1表示启用成功，否则启用失败</returns>
       public int solution(string ID)
       {
           int ret = 0;
           using (EFDbContext dbContext = new EFDbContext())
           {
               var g = dbContext.Users.Find(ID);
               if (g != null)
               {
                   g.Status = 1;
                   dbContext.Entry(g).State = EntityState.Modified;
                   dbContext.SaveChanges();
                   ret = 1;
               }
           }
           return ret;
       }
    }
}
